package com.jshoperxms;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.jshoperxms.controller.mall.backstage.init.InitDataListenter;
import com.jshoperxms.controller.utils.statickey.RedisKey;
import com.jshoperxms.entity.GlobalParamM;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean;
import org.springframework.web.servlet.config.annotation.*;

import java.util.Iterator;
import java.util.Map;

/**
 * @author Arvin.Cao
 * @version 1.0
 * @time 16/3/23 下午12:31
 * @Description SpringMVC配置
 */
//@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/"};

    /**
     * 不拦截的路径
     */
    private final static String[] URL_EXCLUDE_PATTERNS = new String[] { "/mall/user"};

    @Bean
    public InitDataListenter initDataListenter(){
        return new InitDataListenter();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
        super.addResourceHandlers(registry);
    }



    @Bean
    public HibernateJpaSessionFactoryBean sessionFactory() {
        return new HibernateJpaSessionFactoryBean();
    }


    /**
     * 添加JSON的序列化工具
     *
     * @return gson
     */
    @Bean
    public HttpMessageConverter jsonConverters() {
        HttpMessageConverter gson = new GsonHttpMessageConverter();
        return gson;
    }

//    @Override
//    public void addViewControllers(ViewControllerRegistry registry) {
//        registry.addViewController("/error").setViewName("/admin/login.html");
//        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
//    }
//
//    @Override
//    public void configurePathMatch(PathMatchConfigurer configurer) {
//        super.configurePathMatch(configurer);
//        configurer.setUseSuffixPatternMatch(false);
//    }
//
//    @Override
//    protected void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedHeaders("Access-Control-Allow-Origin", "*")
//                .allowedMethods("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE")
//                .allowedOrigins("*");
//    }


    //    @Override
//    protected void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new UserInterceptor()).addPathPatterns("/**").excludePathPatterns(URL_EXCLUDE_PATTERNS);
//        super.addInterceptors(registry);
//    }


}
